#!/usr/bin/env python3

import pickle
import struct

import msgpack
import msgpack_numpy as m
import numpy as np
import redis


def toRedis(r,a,name):
   """Store given Numpy array 'a' in Redis under key 'n'"""
   # encoded = a.tobytes()
   encoded = pickle.dumps(a)
   # Store encoded data in Redis
   r.set(name, encoded)
   return

def fromRedis(r, name):
   """Retrieve Numpy array from Redis key 'n'"""
   encoded = r.get(name)
   # a = np.frombuffer(encoded)
   return encoded

# Create 80x80 numpy array to store
# a0 = np.arange(6400,dtype=np.uint16).reshape(80,80) 
a0 = np.load('/home/liyuan/Programming/python/pathology/pathology/032370b/roi2/Split/proportion/Tr_0.1/Va_0.05/Te_0.85/1/spliteddata.npy', allow_pickle=True)
# Redis connection
a0 = a0.item()
r = redis.Redis(host='localhost', port=6379, db=0)

# Store array a0 in Redis under name 'a0array'
toRedis(r,a0,'roi2_0.05_0.05_0.9_test')

# Retrieve from Redis
a1 = fromRedis(r,'roi2_0.05_0.05_0.9_test')
a1 = pickle.loads(a1)
# np.testing.assert_array_equal(a0,a1)

print('end')
